package com.example.test622.ui.home

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.common.mvi.BaseViewModel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch

class HomeViewModel : BaseViewModel<HomeIntent,HomeState>() {
	init {
		viewModelScope.launch {
			intent.consumeAsFlow().collect{
				when(it){
					is HomeIntent.getVideoType->loadVieoType()
				}
			}
		}
	}
	
	private suspend fun loadVieoType() {
		val video = try {
			val res = service.getVideoType()
			if(res.code == 0){
				HomeState.LoadSuccess(res)
			}else{
				HomeState.loadFail(res.msg)
			}
		}catch (e:Exception){
			HomeState.loadFail(e.message?:"")
		}
		state.postValue(video)
	}
	
}